//
//  BMKProjectNavAnnotationView.m
//  RNIbuildModuleBaiduMap
//
//  Created by Wentao Ye on 10/07/2017.
//  Copyright © 2017 Facebook. All rights reserved.
//


#import "BMKProjectNavAnnotationView.h"

@implementation BMKProjectNavAnnotationView

-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return  self;
}
-(id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if ((self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier])) {
        [self setFrame:CGRectMake(0, 0, kWidth(28), kWidth(28))];
        
        self.backgroundColor = [UIColor clearColor];
        
        [self setUserInteractionEnabled:YES];
        
        
        self.bgIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(28), kHeight(28))];
        [self addSubview:self.bgIcon];
        [self.bgIcon setUserInteractionEnabled:YES];
        
        self.bgTitleArrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(10), kHeight(7))];
        self.bgTitleArrow.image = [UIImage imageNamed:@"icon_projectname_triangle"];
        
        UIImage *img = [UIImage imageNamed:@"icon_project_nav_bg"];
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, img.size.width * 0.5 ,0, img.size.width * 0.5) resizingMode:UIImageResizingModeStretch];
        self.bgTitle = [[UIImageView alloc] initWithFrame:CGRectMake(-((kWidth(132) / 2.0 - kWidth(28) / 2.0)), 0, 0, kHeight(48))];
        
        self.bgTitle.image = img;
        
        [self addSubview:self.bgTitle];
        [self addSubview:self.bgTitleArrow];
        self.lblProjectName = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, 0, self.bgTitle.frame.size.height - 10)];
        self.lblProjectName.textColor = [ Util  colorWithHexString:@"#3f3f3f"];
        self.lblProjectName.numberOfLines = 2;
        self.lblProjectName.textAlignment = NSTextAlignmentCenter;
        self.lblProjectName.lineBreakMode = NSLineBreakByTruncatingTail;
        self.lblProjectName.font = [UIFont systemFontOfSize:12.f];
        self.lblProjectName.backgroundColor = [UIColor clearColor];
        [self.bgTitle addSubview:self.lblProjectName];
        
        self.btnNav = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_nav"]];
        self.btnNav.frame = CGRectMake(0, 0, kWidth(31), kHeight(31));
        [self.bgTitle addSubview:self.btnNav];
        
        self.splitView  = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(0.5), kHeight(28))];
        self.splitView.backgroundColor = [ Util  colorWithHexString:@"#bababa"];
        [self.bgTitle addSubview:self.splitView];
        
        [self.bgTitle setUserInteractionEnabled:YES];
        [self.lblProjectName setUserInteractionEnabled:YES];
        
        UITapGestureRecognizer *tapRecg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
        [self.btnNav addGestureRecognizer:tapRecg2];
    }
    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        CGPoint tempoint = [self.btnNav convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.btnNav.bounds, tempoint))
        {
            view = self.btnNav;
        }
    }
    return view;
}

-(void)viewTapped:(UITapGestureRecognizer *)sender
{
    [self.delegate BMKProjectNavAnnotationView:self clickViewWithData:self.annotation];
}

-(void) displayProjectName:(NSString *)projectName
{
    if(projectName)
    {
        CGSize size = [projectName sizeWithFont:self.lblProjectName.font constrainedToSize:CGSizeMake(kWidth(150),  self.lblProjectName.frame.size.height) lineBreakMode:NSLineBreakByTruncatingTail];
        
        double bgWidth = size.width + 20.f + (self.btnNav.frame.size.width + 10);
        self.bgTitle.frame = CGRectMake(-((bgWidth / 2.0 - kWidth(28) / 2.0)), -kHeight(48), bgWidth, self.bgTitle.frame.size.height);
        
        self.splitView.frame = CGRectMake(self.bgTitle.frame.size.width - self.btnNav.frame.size.width - 10, (self.bgTitle.frame.size.height - self.splitView.frame.size.height) / 2.f, self.splitView.frame.size.width, self.splitView.frame.size.height);
        
        self.btnNav.frame = CGRectMake(self.bgTitle.frame.size.width - self.btnNav.frame.size.width - 10, (self.bgTitle.frame.size.height - self.btnNav.frame.size.height) / 2.f, kWidth(31), kHeight(31));
        self.lblProjectName.text = projectName;
        self.lblProjectName.frame = CGRectMake(self.lblProjectName.frame.origin.x, (self.bgTitle.frame.size.height - size.height) / 2.f ,size.width, size.height);
        self.bgTitleArrow.frame = CGRectMake(kWidth(14.f)-self.bgTitleArrow.frame.size.width / 2.f, self.bgTitle.frame.origin.y + self.bgTitle.frame.size.height - kHeight(5), kWidth(10), kHeight(7));
    }
}
@end
